package com.hezuti.common.structure.tree;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class TreeNode implements Serializable {
	protected String name = "";
	protected Object value = "";
	protected TreeNode parent = null;
	protected Map<String, List<TreeNode>> children = new LinkedHashMap();
	
	public TreeNode() {
		
	}
	
	public TreeNode(String name) {
		this.name = name;
	}
	
	public TreeNode(String name, Object value) {
		this.name = name;
		this.value = value;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setValue(Object value) {
		this.value = value;
	}
	
	public void setParent(TreeNode node) {
		this.parent = node;
	}
	
	public String getName() {
		return name;
	}
	
	public String getValue() {
		return (String) value;
	}
	
	public TreeNode getParent() {
		return parent;
	}
	
	public boolean isLeaf() {
		return children.isEmpty();
	}
	
	public void addChild(TreeNode node) {
		List list = children.get(node.getName());
		if(list == null) {
			list = new ArrayList(1);
			children.put(node.getName(), list);
		}
		node.setParent(this);
		list.add(node);
	}
	
	public List getChild(String name) {
		return children.get(name);
	}
	
	public Map getChildren() {
		return children;
	}
}